﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using System.Collections.Generic;

namespace ActivityTracker.Library
{
    public class YearlyChart
    {
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isoStream;
        XDocument xdoc;

        public List<ChartProp> LoadProgressData(string actId)
        {
            List<ChartProp> progressChartList = new List<ChartProp>();
            try
            {
                if (isoStore.FileExists("ActivityTracker/TotalDurations.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/TotalDurations.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var activitylist = (from t in xdoc.Descendants("yearlyduration") select t);
                    foreach (XElement vx in activitylist.Elements())
                    {
                        foreach (XElement te in vx.Elements().Where(x => x.Attribute("actId").Value == actId))
                        {
                            ChartProp cp = new ChartProp();
                            double avg = Convert.ToInt32(te.Attribute("totDur").Value) / Convert.ToInt32(te.Attribute("count").Value);
                            cp.ActYear = (vx.Name.LocalName).Substring(1, 4);
                            cp.ActDur = avg;
                            progressChartList.Add(cp);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadProgressData Method In ActivityTracker.Library.Charts.YearlyChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                throw exnew;
            }
            return progressChartList;
        }

        public List<ChartProp> LoadTimeSpentChartdata(string Year, ChartType Type)
        {
            List<ChartProp> timeSpentChartList = new List<ChartProp>();
            try
            {
                string selectedyear = "";
                selectedyear = "y" + Year;
                if (isoStore.FileExists("ActivityTracker/TotalDurations.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/TotalDurations.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var activitylist = (from t in xdoc.Descendants("yearlyduration") select t);
                    double othersDuration = 0;
                    if (Type == ChartType.Pie)
                    {
                        double totalduration = 0;
                        totalduration = (from list in activitylist.Descendants(selectedyear).Elements()
                                         select Convert.ToInt32(list.Attribute("totDur").Value)).Sum();
                        ChartProp othercp = new ChartProp();
                        foreach (var activ in activitylist.Descendants(selectedyear).Elements())
                        {
                            ChartProp cp = new ChartProp();
                            cp.ActDur = Convert.ToInt32(activ.Attribute("totDur").Value);
                            if ((cp.ActDur * 100 / totalduration) <= 5)
                            {
                                othersDuration += cp.ActDur;
                            }
                            else
                            {
                                cp.ActName = activ.Attribute("actName").Value;
                                timeSpentChartList.Add(cp);
                            }
                        }
                        othercp.ActName = "Other Activities";
                        othercp.ActDur = othersDuration;
                        if (othersDuration > 0)
                            timeSpentChartList.Add(othercp);
                    }
                    if (Type == ChartType.Bar)
                    {
                        ChartProp othercp = new ChartProp();
                        foreach (var activ in activitylist.Descendants(selectedyear).Elements())
                        {
                            ChartProp cp = new ChartProp();
                            cp.ActDur = Convert.ToInt32(activ.Attribute("totDur").Value);
                            cp.ActName = activ.Attribute("actName").Value;
                            if(cp.ActDur!=0)
                            timeSpentChartList.Add(cp);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadYearlyChartData Method In DisplayyearlyTimespentChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                exnew.Data.Add("pivotname", Year);
                throw exnew;
            }
            return timeSpentChartList;
        }

        public List<ChartProp> LoadCategoryTimeSpentChartdata(string Year, ChartType Type)
        {
            List<ChartProp> timeSpentChartList = new List<ChartProp>();
            try
            {
                string selectedyear = "";
                selectedyear = "y" + Year;
                if (isoStore.FileExists("ActivityTracker/CategoryTotalDurations.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/CategoryTotalDurations.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var activitylist = (from t in xdoc.Descendants("yearlyduration") select t);
                    double othersDuration = 0;
                    if (Type == ChartType.Pie)
                    {
                        double totalduration = 0;
                        totalduration = (from list in activitylist.Descendants(selectedyear).Elements()
                                         select Convert.ToInt32(list.Attribute("totDur").Value)).Sum();
                        ChartProp othercp = new ChartProp();
                        foreach (var activ in activitylist.Descendants(selectedyear).Elements())
                        {
                            ChartProp cp = new ChartProp();
                            cp.ActDur = Convert.ToInt32(activ.Attribute("totDur").Value);
                            if ((cp.ActDur * 100 / totalduration) <= 5)
                            {
                                othersDuration += cp.ActDur;
                            }
                            else
                            {
                                cp.CatName= activ.Attribute("catname").Value;
                                timeSpentChartList.Add(cp);
                            }
                        }
                        othercp.CatName = "Other Activities";
                        othercp.ActDur = othersDuration;
                        if (othersDuration > 0)
                            timeSpentChartList.Add(othercp);
                    }
                    if (Type == ChartType.Bar)
                    {
                        ChartProp othercp = new ChartProp();
                        foreach (var activ in activitylist.Descendants(selectedyear).Elements())
                        {
                            ChartProp cp = new ChartProp();
                            cp.ActDur = Convert.ToInt32(activ.Attribute("totDur").Value);
                            cp.CatName = activ.Attribute("catname").Value;
                            if (cp.ActDur != 0)
                                timeSpentChartList.Add(cp);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadYearlyChartData Method In DisplayyearlyTimespentChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                exnew.Data.Add("pivotname", Year);
                throw exnew;
            }
            return timeSpentChartList;
        }
    }
}
